import Taro, { Component } from '@tarojs/taro'
import { View, ScrollView } from '@tarojs/components'
import { DNavBar, DFinancingRecord, DItemError } from '@/components'
import './financingrecord.scss'
import http from '@/api'
export default class FinancingrecordBox extends Component {

    constructor(props) {
        super(props)
        this.state = {
            title: '代购额度申请记录',
            obj: {
                page: 1,
                limit: 10
            },
            scrollStyle: window.screen.availHeight,
            footerStatus: false,
            list: [],
            totalPage: 1
        }
    }
    componentWillMount() {
        this.applyListRecord()
    }
    applyListRecord() {
        let { page, limit } = this.state.obj
        let { list, totalPage } = this.state
        let obj = {
            page,
            limit
        }
        if (page > totalPage) {
            this.setState({
                footerStatus: true
            })
            return
        }
        http.applyListRecord(obj).then(res => {
            this.setState({
                list: [...list, ...res.list],
                totalPage: res.totalPage || 1
            })
        })
    }
    onScroll = () => {
        this.setState({
            obj: {
                ...this.state.obj,
                page: this.state.obj.page + 1
            }
        }, () => {
            this.applyListRecord()
        })
    }

    render() {
        const { title, list, scrollStyle, footerStatus } = this.state
        return (
            <View className="b_financingrecord">
                <DNavBar title={title}></DNavBar>
                <View className="b_financingrecord__box">
                    <View className="b_financingrecord__content">
                        <ScrollView
                            scrollY
                            scrollWithAnimation
                            lowerThreshold='20'
                            style={`height:${scrollStyle - 50}PX`}
                            onScrollToLower={this.onScroll}>
                            {list && list.map((item, i) => {
                                return <DFinancingRecord key={i} money={'+' + item.mqQuota + '万'} time={item.createTime} status={item.status}></DFinancingRecord>
                            })
                            }
                            {
                                footerStatus && <View className='b_financingrecord__more'>暂无更多</View>
                            }
                        </ScrollView>
                    </View>
                    {
                        list.length === 0 && <DItemError></DItemError>
                    }
                </View>
            </View>
        )
    }
}
